home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c2 / cgauss.hpp < prev    next >
C/C++ Source or Header  |  1992-06-25  |  3KB  |  79 lines

  1. //---------------------------------------------------------------
  2. //    cgauss.hpp, definition of the cGAUSSIAN class
  3. //    ---------------------------------------------
  4. //    
  5. //    This class exists to produce streams of random
  6. //    variables whose distribution is Gaussian.
  7. //    
  8. //    To construct an instance, declare a variable
  9. //    with its mean and variance...
  10. //    
  11. //        cGAUSSIAN G( 0.0, 1.0 );  // unit normal distribution
  12. //    
  13. //    This instance can, and probably should, be a single
  14. //    global variable servicing all calls with the same
  15. //    mean/variance.
  16. //        
  17. //    Once the variable has been set up, individual random
  18. //    numbers can be obtained using the "next" method...
  19. //    
  20. //        double x = G.next();  // "x" gets a random number
  21. //        
  22. //    To gang-load an array of numbers, use the "vector" 
  23. //    method...
  24. //    
  25. //        double v[ 100 ];
  26. //        G.vector( 0, 1, v, 100 );  // 100 elements of v
  27. //                                   // are filled with random
  28. //                                   // numbers whose mean is 0
  29. //                                   // and whose variance is 1
  30. //
  31. //    NOTE:  This file assumes that the application has
  32. //           included the following files...
  33. //                 stdlib.h,   for rand, RAND_MAX
  34. //                   math.h,   for cos, sqrt
  35. //                 values.h    for MINDOUBLE
  36. //           
  37. //
  38. //    HISTORY:  6/92 Developed (GJV)
  39. //--------------------------------------------------------------
  40. //
  41. //
  42. //
  43. //
  44. //--------------------------------------------------------------
  45. #ifndef RAND_MAX
  46. #error CGAUSS.HPP:  stdlib, math, and values must be included...
  47. #endif
  48. class cGAUSSIAN {
  49. private:
  50.     double mean;
  51.     double variance;
  52. protected:
  53. public:
  54.     cGAUSSIAN( double _mean, double _variance ) {
  55.         mean     = _mean;
  56.         variance = _variance;
  57.     }
  58.     double next( void ) {
  59.         double y;
  60.         do y = ( double )( rand() % RAND_MAX ) / ( double )RAND_MAX;
  61.         while( y < MINDOUBLE * 1e11 );
  62.         double z = ( double )( rand( ) % RAND_MAX ) /( double )RAND_MAX;
  63.         return( sqrt( -2.0 * log( y ) ) *
  64.                 cos( 6.2831853072 * z ) *
  65.                 sqrt( variance ) + mean );
  66.     }
  67.     void vector( double *v, int n ) {
  68.        while( n-- > 0 ) {
  69.               v[ n ] = next();
  70.        }
  71.     }
  72. };
  73. //--------------------------------------------------------------
  74. //
  75. //
  76. //
  77. //
  78. //--------------------------------------------------------------
  79.